package com.slkj.shilixiaoyuanapp.ui.ZhongHeXiaoXue.adapter;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.GradeEntity;
import com.slkj.shilixiaoyuanapp.view.dialog.BooheeRulerDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created  on 2018-09-07
 *
 * @author 唐开阔
 * @describe 分级分组-班级-适配器
 */
public class GradeMonthAdapter extends BaseMultiItemQuickAdapter<GradeEntity, BaseViewHolder> {
    List<GradeEntity> data = new ArrayList<>();
    BaseActivity context;

    public GradeMonthAdapter(BaseActivity context, List<GradeEntity> data) {
        super(data);
        this.data = data;
        this.context = context;
        addItemType(GradeEntity.TYPE_GROUP, R.layout.item_grade_select);
        addItemType(GradeEntity.TYPE_CONTENT, R.layout.item_grade_select1);
    }

    @Override
    protected void convert(BaseViewHolder helper, GradeEntity item) {
        if (getItemViewType(helper.getAdapterPosition()) == GradeEntity.TYPE_GROUP) {
            bindGroupData(helper.getAdapterPosition(), helper, item);
        } else if (getItemViewType(helper.getAdapterPosition()) == GradeEntity.TYPE_CONTENT) {
            bindContentData(helper.getAdapterPosition(), helper, item);
        }
    }

    /**
     * 绑定班级数据
     *
     * @param adapterPosition
     * @param helper
     * @param item
     */
    private void bindContentData(int adapterPosition, BaseViewHolder helper, GradeEntity item) {
        helper.setText(R.id.tv_grademonth, item.getGrade())
                .itemView.setOnClickListener(v -> {

                }
        );


    }

    /**
     * 绑定年级分组数据
     *
     * @param pos
     * @param helper
     * @param groupEntity
     */
    private void bindGroupData(int pos, BaseViewHolder helper, GradeEntity groupEntity) {
        helper.setText(R.id.tv_grademonth, groupEntity.getYear());
        ImageView imgOrientation = helper.getView(R.id.img_orientation);
        imgOrientation.setImageResource(groupEntity.isExpand() ? R.drawable.ic_up : R.drawable.ic_down);
        helper.itemView.setOnClickListener(v -> {
            if (groupEntity.isExpand()) {

                imgOrientation.setImageResource(R.drawable.ic_down);
                collapse(pos, groupEntity);
            } else {
                imgOrientation.setImageResource(R.drawable.ic_up);
                expand(pos, groupEntity);
            }
        });
    }

    /**
     * 展开
     *
     * @param position
     * @return
     */
    public void expand(int position, GradeEntity groupEntity) {
        List list = groupEntity.getYearClass();
        mData.addAll(position + 1, list);
        groupEntity.setExpand(true);
        notifyDataSetChanged();
    }

    /**
     * 收缩
     *
     * @param position
     * @return
     */
    public void collapse(int position, GradeEntity groupEntity) {
        int subItemCount = groupEntity.getYearClass().size();
        for (int i = 0; i < subItemCount; i++) {
            mData.remove(position + 1);
        }
        groupEntity.setExpand(false);
        notifyDataSetChanged();
    }
}
